sampler2D rectBlurEffect : register(S0);


float2 upperLeftCorner : register(C0);
float2 lowerRightCorner : register(C1);

float Angle : register(C2);
float BlurAmount = 5;
float BlurAmount2 : register(C3);
float PI = 3.14159265358979323846;
float EPSILON = 0.0001;

float ComputeGaussian(float x,float y)
{
	// float theta = 2.0f + EPSILON; //float.Epsilon;

	// return theta = (float)((1.0 / sqrt(2 * PI * theta)) *
	// 	exp(-(n * n) / (2 * theta * theta)));
	return exp(-(x*x + y*y)/(2*BlurAmount*BlurAmount))
					/(BlurAmount*BlurAmount*2*PI);
}

float4 gaussianblur(float2 texCoord: TEXCOORD0) : COLOR
{

	float4x4 gauss;
	float4 color = 0.0f;
	float re=1024.0f;
	float totalWeights=0;

	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			float weight=ComputeGaussian(i,j);
			gauss[i][j]=weight;
			float2 delta= {i/re , j/re};
			color += tex2D(rectBlurEffect, texCoord + delta) * weight;
			totalWeights+=weight;
		}
	}
	
	for(i=2;i>=0;i--)
	{
		for(int j=2;j>=0;j--)
		{
			totalWeights+=gauss[i][j];
			float2 delta= {(i-3)/re , (i-3)/re};
			color += tex2D(rectBlurEffect, texCoord + delta) * gauss[i][j];
			totalWeights+=gauss[i][j];
		}
	}

	return color/totalWeights;
}

// float4 directionalBlur(float2 uv : TEXCOORD) : COLOR
// {
// 	float4 c = 0;
// 	float rad = Angle * 0.0174533f;
// 	float xOffset = cos(rad);
// 	float yOffset = sin(rad);

// 	for (int i = 0; i < 12; i++)
// 	{
// 		uv.x = uv.x - BlurAmount * xOffset;
// 		uv.y = uv.y - BlurAmount * yOffset;
// 		c += tex2D(rectBlurEffect, uv);
// 	}
// 	c /= 12;

// 	return c;
// }

float4 main(float2 uv : TEXCOORD) : COLOR
{

	if (uv.x < upperLeftCorner.x || uv.y < upperLeftCorner.y || uv.x > lowerRightCorner.x || uv.y > lowerRightCorner.y)
	{
		return tex2D(rectBlurEffect, uv);
	}

	return gaussianblur(uv);
}